package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderGetWaitRobVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.help.SaasListHelp;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/4/8 10:12 AM
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/workOrder/search")
public class MobileWorkOrderSearchController {
    @Resource
    private SaasListHelp saasListHelp;

    /**
     * Description: 获取待抢单数
     * @param baseDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/5/13 19:44
     * @since v1.0
     */
    @RequestMapping(value = "/getWaitRob", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getWaitRob(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<WorkOrderGetWaitRobVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), null, baseDTO.getDistributorMark());
                WorkOrderGetWaitRobVO  workOrderGetWaitRobVO = new WorkOrderGetWaitRobVO();
                workOrderGetWaitRobVO.setBusinessType(XbbRefTypeEnum.WORK_ORDER_ROB.getCode());
                if (baseDTO.getLoginUser().isMaxDataPermission() || baseDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.WORK_ORDER_VIEW.getAlias())){
                    workOrderGetWaitRobVO.setPermission(true);
                    Long count = saasListHelp.getWaitRob(baseDTO.getLoginUser());
                    workOrderGetWaitRobVO.setTotal(count);
                }else {
                    workOrderGetWaitRobVO.setPermission(false);
                    workOrderGetWaitRobVO.setTotal(0L);
                }
                response = new XbbResponse<>(workOrderGetWaitRobVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
